package com.htlh.filedemo;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.htlh.filedemo.bean.Book;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainActivity";
    private android.widget.TextView textView;
    private android.widget.Button button;
    private Button btninsert;
    private Button btnupdate;
    private Button btncreate;
    private Button btndelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.btndelete = (Button) findViewById(R.id.btn_delete);
        this.btncreate = (Button) findViewById(R.id.btn_create);
        this.btnupdate = (Button) findViewById(R.id.btn_update);
        this.btninsert = (Button) findViewById(R.id.btn_insert);
        this.button = (Button) findViewById(R.id.button);

     /*   News news = new News();
        news.setTitle("新闻标题");
        news.setContent("这是一条新闻内容");
        news.setPublishDate(new Date());
        news.save();*/
       /* Comment comment1 = new Comment();
        comment1.setContent("好评！");
        comment1.setPublishDate(new Date());
        comment1.save();
        Comment comment2 = new Comment();
        comment2.setContent("赞一个");
        comment2.setPublishDate(new Date());
        comment2.save();
        News news = new News();

        news.getCommentList().add(comment1);
        news.getCommentList().add(comment2);
        news.setTitle("第二条新闻标题");
        news.setContent("第二条新闻内容");
        news.setPublishDate(new Date());
        news.setCommentCount(news.getCommentList().size());
        news.save();

        ContentValues values = new ContentValues();
        values.put("title", "今日iPhone6发布");
        DataSupport.update(News.class, values, 2);*/

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase database = Connector.getDatabase();
            }
        });

        btninsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setAuthor("acefa");
                book.setName("android大神之路");
                book.setPages(400);
                book.setPrice(50.0);
                book.save();
            }
        });

        btnupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setPages(500);
                book.setPrice(68.0);
                book.updateAll("name = ? and author = ?", "android大神之路", "acefa");
            }
        });

        btndelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class, "price < ?", "51");
            }
        });

        btncreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> bookList = DataSupport.findAll(Book.class);
                for (Book book : bookList) {
                    Log.d(TAG,book.getName());
                    Log.d(TAG,book.getAuthor());
                    Log.d(TAG,book.getPrice() + "");
                    Log.d(TAG,book.getPages() + "");
                }
            }
        });


    }
}
